{ TSM MA of Ratio 
  Copyright 2011, P.J.Kaufman. All rights reserved. }
   
  inputs: period(40), voladj(true);
  vars:	 ratio(0), MA(0), entry1(0), entry2(0), size1(1), size2(1), PL1(0), PL2(0), 
  			 cumPL1(0), cumPL2(0), signal(0), investment(10000), newsize1(0), newsize2(0),
  			 MA1signal(0), MA2signal(0), MA1size(0), MA2size(0), MA1PL(0), MA2PL(0), 
  			 MA1cumPL(0), MA2cumPL(0), MA1entry(0), MA2entry(0), MA1(0), MA2(0);
  
   If Currentbar = 1 then begin
		print (file("c:\TSM5\MA_of_ratio.csv"), "date,ratio,MA,signal,size1,entry1,close1,",
				"size2,entry2,close2,PL1,PL2,ratioPL1,ratioPL2,",
				"ratioPL,signal1,size1,entry1,",
				"signal2,size2,entry2,PL1,PL2,cumPL1,cumPL2,combPL"); 
		end;
   
   ratio = close/Close of data2;
   MA = average(ratio,period);
{ track MA of both series }
	MA1 = average(close,period);
	MA2 = average(close of data2,period);   
   
   If voladj then begin
   	newsize1 = investment/(AvgTrueRange(period)*bigpointvalue);
   	newsize2 = investment/(AvgTrueRange(period) of data2*bigpointvalue of data2);
   	end;
  
	If signal <> 1 and MA > MA[1] then begin
			signal = 1;
			entry1 = close;
			entry2 = Close of data2;
			If voladj then begin
				size1 = newsize1;
				size2 = newsize2;
				end;
			Buy ("Lratio") this bar on close;
			End
		Else if signal <> -1 and MA < MA[1] then begin
			signal = -1;
			entry1 = close;
			entry2 = Close of data2;
			If voladj then begin
				size1 = newsize1;
				size2 = newsize2;
				end;
			Sell ("Sratio") this bar on close;
		end;
		
{ individual signals }
	If MA1signal <> 1 and MA1 > MA1[1] then begin
			MA1signal = 1;
			MA1size = newsize1;
			MA1entry = close;
			end
		else if MA1signal <> -1 and MA1 < MA1[1] then begin
			MA1signal = -1;
			MA1size = newsize2;
			MA1entry = close;
		end;
	If MA2signal <> 1 and MA2 > MA2[1] then begin
			MA2signal = 1;
			MA2size = newsize2;
			MA2entry = Close of data2;
			end
		else if MA2signal <> -1 and MA2 < MA2[1] then begin
			MA2signal = -1;
			MA2size = newsize2;
			MA2entry = Close of data2;
		end;
		

{ ratio performance }		
	PL1 = signal[1]*size1[1]*(Close - close[1])*bigpointvalue;
	PL2 = -signal[1]*size2[1]*(Close - close[1])*bigpointvalue of data2;
	cumPL1 = cumPL1 + PL1;
	cumPL2 = cumPL2 + PL2;
{ individual series }	
	MA1PL = MA1signal[1]*MA1size[1]*(Close - close[1])*bigpointvalue;
	MA2PL = MA2signal[1]*MA2size[1]*(Close of data2 - close[1] of data2)*bigpointvalue of data2;
	MA1cumPL = MA1cumPL + MA1PL;
	MA2cumPL = MA2cumPL + MA2PL;
	print (file("c:\TSM5\MA_of_ratio.csv"), date:8:0, ",",  ratio:3:3, ",", MA:3:3, ",",
			signal:3:0, ",", size1:4:0, ",", entry1:5:3, ",",
			close:5:3, ",", size2:4:0, ",", entry2:5:3, ",", close of data2:5:3, ",", 
			PL1:6:0, ",", PL2:6:0, ",",cumPL1:8:0, ",", cumPL2:8:0, ",", cumPL1+cumPL2:8:0, ",",
			MA1signal:3:0, ",", MA1size:4:0, ",", MA1entry:5:3, ",", 
			MA2signal:3:0, ",", MA2size:4:0, ",", MA2entry:5:3, ",", 
			MA1PL:6:0, ",", MA2PL:6:0, ",", 
			MA1cumPL:8:0, ",", MA2cumPL:8:0, ",", MA1cumPL+MA2cumPL:8:0);